#include "DemoApp.h"
#include "ImageResources.h"
//extern ID2D1Bitmap *bmpTemp2;
//extern TCHAR exeDirPath2[256];
//extern WCHAR dirPath2[256];
DemoApp::DemoApp() {
}


DemoApp::~DemoApp() {
}

HRESULT DemoApp::CreateDeviceIndependentResources() {
	HRESULT hr = NULL;
	CoInitialize(NULL);
	hr = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &m_pDirect2dFactory);
	hr = CoCreateInstance(CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, IID_IWICImagingFactory, (LPVOID*)&p_pImagingFactory);
	//LoadBitmapFromFile(m_pRenderTarget, p_pImagingFactory, L"C:\\Users\\Administrator\\Desktop\\lufei.jpg", 0, 0, &p_pBitmap);
	return hr;
}

HRESULT DemoApp::LoadBitmapFromFile(
	ID2D1RenderTarget *pRenderTarget,
	IWICImagingFactory *pIWICFactory,
	PCWSTR uri,
	UINT destinationWidth,
	UINT destinationHeight,
	ID2D1Bitmap **ppBitmap
	) {
	HRESULT hr = S_OK;

	IWICBitmapDecoder *pDecoder = NULL;
	IWICBitmapFrameDecode *pSource = NULL;
	IWICStream *pStream = NULL;
	IWICFormatConverter *pConverter = NULL;
	IWICBitmapScaler *pScaler = NULL;

	hr = pIWICFactory->CreateDecoderFromFilename(
		L"..\\images\\lufei.jpg",
		NULL,
		GENERIC_READ,
		WICDecodeMetadataCacheOnLoad,
		&pDecoder
		);
	if (SUCCEEDED(hr)) {

		// Create the initial frame.
		hr = pDecoder->GetFrame(0, &pSource);
		//UINT uiFrameCount = 0;
		//hr = pDecoder->GetFrameCount(&uiFrameCount);
	}
	if (SUCCEEDED(hr)) {
		hr = pIWICFactory->CreateFormatConverter(&pConverter);
	}
	UINT originalWidth, originalHeight;
	if (SUCCEEDED(hr)) {
		hr = pSource->GetSize(&originalWidth, &originalHeight);
	}
	hr = pIWICFactory->CreateBitmapScaler(&pScaler);
	if (SUCCEEDED(hr)) {
		hr = pScaler->Initialize(pSource, originalWidth, originalHeight, WICBitmapInterpolationModeCubic);
	}
	if (SUCCEEDED(hr)) {
		hr = pConverter->Initialize(pScaler, 
			GUID_WICPixelFormat32bppPBGRA, 
			WICBitmapDitherTypeNone, 
			NULL, 0.f, WICBitmapPaletteTypeMedianCut);
		/*hr = pConverter->Initialize(
			pSource,
			GUID_WICPixelFormat32bppPBGRA,
			WICBitmapDitherTypeNone,
			NULL,
			0.f,
			WICBitmapPaletteTypeMedianCut
			);*/

	}

	if (SUCCEEDED(hr)) {
		hr = pRenderTarget->CreateBitmapFromWicBitmap(
			pConverter,
			NULL,
			ppBitmap
			);
	}
	return hr;
}

HRESULT DemoApp::CreateDeviceResources() {
	HRESULT hr = S_OK;
	if (!m_pRenderTarget) {
		RECT rc;
		GetClientRect(m_hwnd, &rc);
		D2D1_SIZE_U size = D2D1::SizeU(
			rc.right - rc.left,
			rc.bottom - rc.top
			);
		hr = m_pDirect2dFactory->CreateHwndRenderTarget(
			D2D1::RenderTargetProperties(),
			D2D1::HwndRenderTargetProperties(m_hwnd, size),
			&m_pRenderTarget
			);
		if (SUCCEEDED(hr)) {
			initResources(m_pRenderTarget);
			//LoadBitmapFromFile(m_pRenderTarget, p_pImagingFactory, L"images/lufei.jpg", 0, 0, &p_pBitmap);
		}
		if (SUCCEEDED(hr)) {
			hr = m_pRenderTarget->CreateSolidColorBrush(
				D2D1::ColorF(D2D1::ColorF::LightSlateGray),
				&m_pLightSlateGrayBrush);
		}
		if (SUCCEEDED(hr)) {
			hr = m_pRenderTarget->CreateSolidColorBrush(
				D2D1::ColorF(D2D1::ColorF::CornflowerBlue),
				&m_pCornflowerBlueBrush);
		}
		if (SUCCEEDED(hr)) {
			hr = m_pRenderTarget->CreateBitmapBrush(p_pBitmap, &m_pBitmapBrush);
		}
	}
	return hr;
}

void DemoApp::DiscardDeviceResources() {
	SafeRelease(&m_pRenderTarget);
	SafeRelease(&m_pLightSlateGrayBrush);
	SafeRelease(&m_pCornflowerBlueBrush);
}

HRESULT DemoApp::OnRender() {
	HRESULT hr = S_OK;
	hr = CreateDeviceResources();
	if (SUCCEEDED(hr)) {
		m_pRenderTarget->BeginDraw();
		m_pRenderTarget->SetTransform(D2D1::Matrix3x2F::Identity());
		m_pRenderTarget->Clear(D2D1::ColorF(D2D1::ColorF::White));
		D2D1_SIZE_F rtSize = m_pRenderTarget->GetSize();
		int width = static_cast<int> (rtSize.width);
		int height = static_cast<int> (rtSize.height);
		for (int x = 0; x < width; x += 10) {
			m_pRenderTarget->DrawLine(
				D2D1::Point2F(static_cast<FLOAT>(x), 0.0f),
				D2D1::Point2F(static_cast<FLOAT>(x), rtSize.height),
				m_pLightSlateGrayBrush, 0.5f
				);
		}
		for (int y = 0; y < height; y+=10) {
			m_pRenderTarget->DrawLine(
				D2D1::Point2F(0.0f, static_cast<FLOAT>(y)),
				D2D1::Point2F(rtSize.width, static_cast<FLOAT>(y)),
				m_pLightSlateGrayBrush, 0.5f);
		}
		/*D2D1_RECT_F rectangle1 = D2D1::RectF(
			rtSize.width / 2 - 50.0f,
			rtSize.height / 2 - 50.0f,
			rtSize.width / 2 + 50.0f,
			rtSize.height / 2 + 50.0f
			);
		D2D1_RECT_F rectangle2 = D2D1::RectF(
			rtSize.width / 2 - 100.0f,
			rtSize.height / 2 - 100.0f,
			rtSize.width / 2 + 100.0f,
			rtSize.width / 2 + 100.f);*/
		//m_pRenderTarget->FillRectangle(&rectangle1, m_pLightSlateGrayBrush);
		//m_pRenderTarget->DrawRectangle(&rectangle2, m_pCornflowerBlueBrush);
		//D2D1_RECT_F rcBrushRect = D2D1::RectF(0, 0, 416, 294);
		D2D1_RECT_F rcBrushRect = D2D1::RectF(0, 0, 208, 147);
		D2D1_RECT_F destinationRectangle = D2D1::RectF(0,0,208,147);
		D2D1_RECT_F sourceRectangle = D2D1::RectF(0,0,416,294);
		//m_pRenderTarget->FillRectangle(&rcBrushRect, m_pBitmapBrush);
		m_pRenderTarget->DrawBitmap(bmpTemp, &destinationRectangle, 0.5f, D2D1_BITMAP_INTERPOLATION_MODE_NEAREST_NEIGHBOR, &sourceRectangle);
		//m_pRenderTarget->DrawRectangle(&rcBrushRect, m_pBitmapBrush);
		hr = m_pRenderTarget->EndDraw();
		if (hr == D2DERR_RECREATE_TARGET) {
			hr = S_OK;
			DiscardDeviceResources();
		}

		return hr;
	}
	return hr;
}
